package com.hdsx.netty.tomcatserver.bio;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 占贤银
 * time 2024/8/4 22:51
 * description 生命之灯因热情而点燃，生命之舟因拼搏而前行
 */
public class GPRequest {
    private String method;
    private String url;
    private String protocol;

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public Map<String, String> getParamterMap() {
        return paramterMap;
    }

    public void setParamterMap(Map<String, String> paramterMap) {
        this.paramterMap = paramterMap;
    }

    private Map<String, String> paramterMap = new HashMap(10);

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getParameter(String name) {
        return paramterMap.get(name);
    }

    public GPRequest(InputStream is)
    {
        try {
            byte[] buffer = new byte[1024];
            int len = 0;
            String content = "";
            if ((len = is.read(buffer)) > 0) {
                content = new String(buffer, 0, len);
            }
            String line = content.split("\\n")[0];
            String[] arr = line.split("\\s");
            this.method = arr[0];
            this.protocol = arr[2];
            String[] urlOrParam = arr[1].split("\\?");
            this.url = urlOrParam[0];
            if (urlOrParam.length > 1) {
                handlerRequestParameter(urlOrParam[1]);
            }
            // content = GET /firstServlet?name=zhanxy&age=123 HTTP/1.1
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void handlerRequestParameter(String param) {
        paramterMap.put("name", "123");
    }

    public static void main(String[] args) {
        String str = "name=zhanxy&age=123";
    }
}
